package com.yiyuan.app.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guaike.core.mybatis.plugins.BaseDAO;
import com.yiyuan.app.entity.enums.PrizeStatusEnum;
import com.yiyuan.app.entity.enums.RoomTypeEnum;
import com.yiyuan.app.entity.po.GRecordPrizePO;
import com.yiyuan.app.mapper.GRecordPrizeMapper;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Repository
public class GRecordPrizeDAO extends BaseDAO<GRecordPrizeMapper, GRecordPrizePO> {

    public Page<GRecordPrizePO> findPage(Long userId, Integer pageIndex, Integer pageSize) {
        LambdaQueryWrapper<GRecordPrizePO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GRecordPrizePO::getUserId, userId);
        queryWrapper.orderByDesc(GRecordPrizePO::getId);
        return getBaseMapper().selectPage(new Page<>(pageIndex, pageSize), queryWrapper);
    }


    public List<GRecordPrizePO> findNewMachine(Long userId) {
        LambdaQueryWrapper<GRecordPrizePO> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(userId)) {
            queryWrapper.eq(GRecordPrizePO::getUserId, userId);
        }
        queryWrapper.eq(GRecordPrizePO::getStatus, PrizeStatusEnum.issued.getCode());
        queryWrapper.in(GRecordPrizePO::getRoomType, Arrays.asList(RoomTypeEnum.arcade_machine.getCode(), RoomTypeEnum.recreation.getCode()));
        queryWrapper.orderByDesc(GRecordPrizePO::getId);
        queryWrapper.last("limit 0,20");
        return getBaseMapper().selectList(queryWrapper);
    }
}
